/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.company.management.controller;

import java.io.Serializable;

import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.company.management.entity.EmployeeSchedule;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.company.management.entity.Schedule;
import org.company.management.controller.exceptions.IllegalOrphanException;
import org.company.management.controller.exceptions.NonexistentEntityException;

/**
 *
 * @author Paner
 */
@SuppressWarnings("serial")
public class ScheduleJpaController implements Serializable {

    public ScheduleJpaController() {
    	emf = Persistence.createEntityManagerFactory("Management");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Schedule schedule) {
        if (schedule.getEmployeeScheduleList() == null) {
            schedule.setEmployeeScheduleList(new ArrayList<EmployeeSchedule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<EmployeeSchedule> attachedEmployeeScheduleList = new ArrayList<EmployeeSchedule>();
            for (EmployeeSchedule employeeScheduleListEmployeeScheduleToAttach : schedule.getEmployeeScheduleList()) {
                employeeScheduleListEmployeeScheduleToAttach = em.getReference(employeeScheduleListEmployeeScheduleToAttach.getClass(), employeeScheduleListEmployeeScheduleToAttach.getId());
                attachedEmployeeScheduleList.add(employeeScheduleListEmployeeScheduleToAttach);
            }
            schedule.setEmployeeScheduleList(attachedEmployeeScheduleList);
            em.persist(schedule);
            for (EmployeeSchedule employeeScheduleListEmployeeSchedule : schedule.getEmployeeScheduleList()) {
                Schedule oldScheduleidOfEmployeeScheduleListEmployeeSchedule = employeeScheduleListEmployeeSchedule.getScheduleid();
                employeeScheduleListEmployeeSchedule.setScheduleid(schedule);
                employeeScheduleListEmployeeSchedule = em.merge(employeeScheduleListEmployeeSchedule);
                if (oldScheduleidOfEmployeeScheduleListEmployeeSchedule != null) {
                    oldScheduleidOfEmployeeScheduleListEmployeeSchedule.getEmployeeScheduleList().remove(employeeScheduleListEmployeeSchedule);
                    oldScheduleidOfEmployeeScheduleListEmployeeSchedule = em.merge(oldScheduleidOfEmployeeScheduleListEmployeeSchedule);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Schedule schedule) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Schedule persistentSchedule = em.find(Schedule.class, schedule.getId());
            List<EmployeeSchedule> employeeScheduleListOld = persistentSchedule.getEmployeeScheduleList();
            List<EmployeeSchedule> employeeScheduleListNew = schedule.getEmployeeScheduleList();
            List<String> illegalOrphanMessages = null;
            for (EmployeeSchedule employeeScheduleListOldEmployeeSchedule : employeeScheduleListOld) {
                if (!employeeScheduleListNew.contains(employeeScheduleListOldEmployeeSchedule)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EmployeeSchedule " + employeeScheduleListOldEmployeeSchedule + " since its scheduleid field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<EmployeeSchedule> attachedEmployeeScheduleListNew = new ArrayList<EmployeeSchedule>();
            for (EmployeeSchedule employeeScheduleListNewEmployeeScheduleToAttach : employeeScheduleListNew) {
                employeeScheduleListNewEmployeeScheduleToAttach = em.getReference(employeeScheduleListNewEmployeeScheduleToAttach.getClass(), employeeScheduleListNewEmployeeScheduleToAttach.getId());
                attachedEmployeeScheduleListNew.add(employeeScheduleListNewEmployeeScheduleToAttach);
            }
            employeeScheduleListNew = attachedEmployeeScheduleListNew;
            schedule.setEmployeeScheduleList(employeeScheduleListNew);
            schedule = em.merge(schedule);
            for (EmployeeSchedule employeeScheduleListNewEmployeeSchedule : employeeScheduleListNew) {
                if (!employeeScheduleListOld.contains(employeeScheduleListNewEmployeeSchedule)) {
                    Schedule oldScheduleidOfEmployeeScheduleListNewEmployeeSchedule = employeeScheduleListNewEmployeeSchedule.getScheduleid();
                    employeeScheduleListNewEmployeeSchedule.setScheduleid(schedule);
                    employeeScheduleListNewEmployeeSchedule = em.merge(employeeScheduleListNewEmployeeSchedule);
                    if (oldScheduleidOfEmployeeScheduleListNewEmployeeSchedule != null && !oldScheduleidOfEmployeeScheduleListNewEmployeeSchedule.equals(schedule)) {
                        oldScheduleidOfEmployeeScheduleListNewEmployeeSchedule.getEmployeeScheduleList().remove(employeeScheduleListNewEmployeeSchedule);
                        oldScheduleidOfEmployeeScheduleListNewEmployeeSchedule = em.merge(oldScheduleidOfEmployeeScheduleListNewEmployeeSchedule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = schedule.getId();
                if (findSchedule(id) == null) {
                    throw new NonexistentEntityException("The schedule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Schedule schedule;
            try {
                schedule = em.getReference(Schedule.class, id);
                schedule.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The schedule with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<EmployeeSchedule> employeeScheduleListOrphanCheck = schedule.getEmployeeScheduleList();
            for (EmployeeSchedule employeeScheduleListOrphanCheckEmployeeSchedule : employeeScheduleListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Schedule (" + schedule + ") cannot be destroyed since the EmployeeSchedule " + employeeScheduleListOrphanCheckEmployeeSchedule + " in its employeeScheduleList field has a non-nullable scheduleid field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(schedule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Schedule> findScheduleEntities() {
        return findScheduleEntities(true, -1, -1);
    }

    public List<Schedule> findScheduleEntities(int maxResults, int firstResult) {
        return findScheduleEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	private List<Schedule> findScheduleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Schedule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Schedule findSchedule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Schedule.class, id);
        } finally {
            em.close();
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public int getScheduleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Schedule> rt = cq.from(Schedule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
